package com.woniuxy.service.impl;

import com.woniuxy.beans.pojo.User;
import com.woniuxy.dao.UserMapper;
import com.woniuxy.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User getUserByName(String loginname) {
        return userMapper.selectUserByName(loginname);
    }
    @Override
    public User login(String loginname, String password) {
        Subject subject=SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(loginname,password);
        try {
            subject.login(token);
            Session session=subject.getSession();
            User user=this.getUserByName(loginname);
            session.setAttribute("SESSION_USER",user);
            return user;
        } catch (AuthenticationException e) {
            e.printStackTrace();
        } catch (InvalidSessionException e) {
            e.printStackTrace();
        }
        return null;
    }
}
